package com.violet.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.violet.config.RabbitmqConfig;
import com.violet.domain.emuns.ResultCode;
import com.violet.domain.result.OrderMessage;
import com.violet.domain.result.Result;
import com.violet.socket.OrderMessageSocket;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;

// 监控支付消息
@Component
@RequiredArgsConstructor
public class PaidMonitor {

    private final OrderMessageSocket socket;
    private final ObjectMapper objectMapper;

    @RabbitListener(queues = RabbitmqConfig.QUEUE_PAID)
    public void monitor(Message message) throws JsonProcessingException {

        String orderId = new String(message.getBody());

        Result<OrderMessage> result = new Result<OrderMessage>()
                .setCode(ResultCode.ORDER_PAID.code)
                .setData(OrderMessage.buildOrderPaid(orderId));

        socket.sendMessage(objectMapper.writeValueAsString(result));
    }

}
